/*
 * utils.c
 *
 *  Created on: Jul 22, 2012
 *      Author: baris
 */
#include <assert.h>

#include "locals.h"
#include "../model/datatypes.h"

// #define BUILD_PROCESSED_AXIOM_ID(ax) snprintf((char*) processed_axiom_buffer,19,"%p_%p",ax->lhs,ax->rhs)
#define BUILD_PROCESSED_AXIOM_ID(lhs, rhs) snprintf((char*) processed_axiom_buffer, 19,"%p_%p",lhs, rhs)
// #define BUILD_PROCESSED_AXIOM_ID(lhs, rhs) snprintf((char*) processed_axiom_buffer, 22,"%p",lhs)

void enqueue_to_scheduled_axioms(SubClassAxiom* ax) {
	SubClassAxiom** tmp = realloc(scheduled_axioms, (scheduled_axiom_count + 1) * sizeof(SubClassAxiom*));
	assert(tmp != NULL);
	scheduled_axioms = tmp;
	scheduled_axioms[scheduled_axiom_count] = ax;
	scheduled_axiom_count++;
}

SubClassAxiom* dequeue_from_scheduled_axioms() {
	SubClassAxiom* ax;
	SubClassAxiom** tmp;

	if (scheduled_axiom_count == 0)
		return NULL;

	scheduled_axiom_count--;
	ax = scheduled_axioms[scheduled_axiom_count];
	tmp = realloc(scheduled_axioms, (scheduled_axiom_count) * sizeof(SubClassAxiom*));
	assert(tmp != NULL || scheduled_axiom_count == 0);
	scheduled_axioms = tmp;

	return ax;
}

// For marking an axiom as processed, we add the rhs to the subsumers
// of the lhs. The function returns 1 if it is added, 0 if the rhs was
// already in the list of subsumers of the lhs.
int mark_axiom_processed(SubClassAxiom* ax) {
	 int ret_code;

	J1S(ret_code, ax->lhs->subsumers, (Word_t) ax->rhs);
	if (ret_code == JERR) {
		fprintf(stderr, "could not mark axiom as processed, aborting\n");
		exit(EXIT_FAILURE);
	}
	return ret_code;
}
